/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.fields.combo 
{
	import com.actiplay.form.carrefour.comboBelle.ComboBelle;
	import com.actiplay.form.carrefour.comboBelle.ComboBelleEvent;
	import flash.events.Event;
	import flash.events.FocusEvent;
	/**
	 * To use with ComboBelle by PPJ
	 * @author damien pegoraro
	 */
	public class ComboBelleField extends ComboField
	{		
		protected var _combo  : ComboBelle ; 
		protected var _currentValue : Array ; 	
		
		public function ComboBelleField( combo  : ComboBelle ) 
		{
			_combo = combo ; 			
			_combo.addEventListener(ComboBelleEvent.SELECTED, _handleSelected) ; 
			_combo.addEventListener(FocusEvent.FOCUS_IN, _throwFocusIn ) ;
			_combo.addEventListener(FocusEvent.FOCUS_OUT, _throwFocusOut ) ;
			super() ; 			
		}
		
		override public function setTabIndex(index:int):int 
		{
			_combo.tabChildren = false ; 
			_combo.tabIndex = index ; 
			return super.setTabIndex(index) ; 
		}
		
		
		override public function dispose():void 
		{
			super.dispose();
			if (_combo )
			{
				_combo.removeEventListener(ComboBelleEvent.SELECTED, _handleSelected) ; 
				_combo.removeEventListener(FocusEvent.FOCUS_IN, _throwFocusIn ) ;
				_combo.removeEventListener(FocusEvent.FOCUS_OUT, _throwFocusOut ) ;
			}
		}
		
		private function _handleSelected(e:ComboBelleEvent):void 
		{			
			_currentValue = e.item ; 	
			_notifyChanged() ; 
		}
		
		override public function empty():void 
		{
			_currentValue = null ; 		
			_combo.reset() ; 
			super.empty();
		}
		
		
		override public function getValue():* 
		{		
			if ( _valueProperty && _valueProperty != "" ) return getProperty(_valueProperty) ; 
			else return _currentValue ? _currentValue[0] : null ; 		
		}
		
		public function getProperty( name : String ) : *
		{
			return ( _currentValue && _currentValue[0] ) ? _currentValue[0][name] : null ; 
		}
		
		
		public function getCombo() : ComboBelle
		{
			return _combo ;
		}
	}

}